﻿using AutoMapper;
using Data0426.Base.Common;
using Data0426.Base.Dommain;
using Data0426.Base.Infrastructure;
using Data0426.Base.Read.API.Applaction.Command;
using Data0426.Base.Read.API.DTO;
using MediatR;

namespace Data0426.Base.Read.API.Applaction.Hander
{
    public class ShowFormHander : IRequestHandler<ShowFormTypeCommand, ApiResult<ApiPaging<List<ShowFormDTO>>>>
    {
        private readonly IBaseRequest<FormType> FormTypebaseRequest;
        private readonly IBaseRequest<Indescs> IndescsbaseRequest;
        private readonly IMapper Mapper;

        public ShowFormHander(IBaseRequest<FormType> formTypebaseRequest, IBaseRequest<Indescs> indescsbaseRequest, IMapper mapper)
        {
            FormTypebaseRequest = formTypebaseRequest;
            IndescsbaseRequest = indescsbaseRequest;
            Mapper = mapper;
        }

        public Task<ApiResult<ApiPaging<List<ShowFormDTO>>>> Handle(ShowFormTypeCommand request, CancellationToken cancellationToken)
        {
            var result=new ApiResult<ApiPaging<List<ShowFormDTO>>>();
            var info1 = FormTypebaseRequest.GetAll().Where(x => x.Isdelete == 0).ToList();
            var info2 = IndescsbaseRequest.GetAll().Where(x => x.Isdelete == 0).ToList();
            var liancha = from a in info1
                          join b in info2 on a.IndescsId equals b.Id
                          select new ShowFormDTO()
                          {
                              Id = a.Id,
                              FormTypeName = a.FormTypeName,
                              IndescsId = b.Id,
                              IndescsName = b.IndescsName,
                          };
            var entity = Mapper.Map<List<ShowFormDTO>>(liancha);
            var page=new ApiPaging<List<ShowFormDTO>>();
            page.tocount = entity.Count();
            page.topage = (int)Math.Ceiling(page.tocount * 1.0 / request.pageSize);
            page.data = entity.OrderBy(x => x.Id).Skip((request.pageindex - 1) * request.pageSize).Take(request.pageSize).ToList();
            result.data = page;
            result.code = ApiEnumcode.OK;
            result.meg = "获取成功";
            return Task.FromResult(result);
        }
    }
}
